﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Client
{
    /// <summary>
    /// Interaction logic for Game.xaml
    /// </summary>
    public partial class Game : UserControl
    {
        
        List<Piece> playerOnePieces = new List<Piece>();
        List<Piece> playerTwoPieces = new List<Piece>();

        Point startPos;
        int rectangleSize;
        
        public Game()
        {
            InitializeComponent();
            DrawGame();
        }

        private void DrawGame()
        {
            rectangleSize = Convert.ToInt32((front_canvas.Width < front_canvas.Height) ? front_canvas.Width : front_canvas.Height) / 8;
            DrawBoard(rectangleSize);
            DrawPieces(rectangleSize);

        }


        private void DrawPieces(int rectangleSize)
        {


            for (int n = 0; n < 3;n++ )
                for (int i = 0; i < 4; i++)
                {
                    System.Windows.Shapes.Ellipse circ;
                    circ = new System.Windows.Shapes.Ellipse();
                    circ.Stroke = new SolidColorBrush(Colors.Green);
                    circ.Fill = new SolidColorBrush(Colors.Green);
                    circ.Width = circ.Height = rectangleSize-2;

                    Canvas.SetTop(circ, n * rectangleSize +1);
                    if(n%2==0)
                        Canvas.SetLeft(circ, i * rectangleSize * 2 + 1);
                    else
                        Canvas.SetLeft(circ, i * rectangleSize * 2 + rectangleSize + 1);

                    circ.MouseLeftButtonDown += circ_MouseLeftButtonDown;
                    circ.MouseLeftButtonUp += circ_MouseLeftButtonUp;
                    front_canvas.Children.Add(circ);
                    playerOnePieces.Add(new Piece(circ, false));
                }

            for (int n = 5; n < 8; n++)
                for (int i = 0; i < 4; i++)
                {
                    System.Windows.Shapes.Ellipse circ;
                    circ = new System.Windows.Shapes.Ellipse();
                    circ.Stroke = new SolidColorBrush(Colors.Red);
                    circ.Fill = new SolidColorBrush(Colors.Red);
                    circ.Width = circ.Height = rectangleSize - 2;

                    Canvas.SetTop(circ, n * rectangleSize + 1);
                    if (n % 2 == 0)
                        Canvas.SetLeft(circ, i * rectangleSize * 2 + 1);
                    else
                        Canvas.SetLeft(circ, i * rectangleSize * 2 + rectangleSize + 1);
                    front_canvas.Children.Add(circ);

                    playerTwoPieces.Add(new Piece(circ, false));
                }
        
            
        }

        void circ_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            startPos = e.MouseDevice.GetPosition(this);
        }

        void circ_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {

            Point endPos = e.MouseDevice.GetPosition(this);
            Piece pickedPiece =  (Piece)sender;

        }
        private void DrawBoard(int rectangleSize)
        {
           System.Windows.Shapes.Rectangle rect;
            rect = new System.Windows.Shapes.Rectangle();
            rect.Stroke = new SolidColorBrush(Colors.Black);
            rect.Fill = new SolidColorBrush(Colors.Black);
            
            
            //hogy egyenlő oldalú legyenek a mezők
            
            rect.Width = rect.Height = rectangleSize*8;
            Canvas.SetLeft(rect, 0);
            Canvas.SetTop(rect, 0);
            front_canvas.Children.Add(rect);

  
            
            
            
            for (int i = 0; i < 8;i++ )
                for (int n = 0; n < 4; n++)
                {
                    rect = new System.Windows.Shapes.Rectangle();
                    rect.Stroke = new SolidColorBrush(Colors.White);
                    rect.Fill = new SolidColorBrush(Colors.White);
                    rect.Width = rect.Height = rectangleSize;
                    Canvas.SetLeft(rect, i*rectangleSize);
                    if(i%2==0)
                        Canvas.SetTop(rect, n * rectangleSize *2);
                    else
                        Canvas.SetTop(rect, n * rectangleSize * 2 + rectangleSize);
                    front_canvas.Children.Add(rect);

                }

        }
    }
}
